/*
(c) Taras Kudla, 2009

t@ettera.com
*/


/*
 * Gepard Object Definition
 */

var Gepard = new Object();

/*
 * Params Setting
 */
Gepard.Caller_ID	= null;
Gepard.Caller_Pamas	= null;
Gepard.API			= ___+____;
Gepard.FlagToBuffer = false;
Gepard.QueryBuffer	= "empty";

/*
 * Gepard Methods
 */
Gepard.Redirect= function (link) {
		
	window.location=link;
}


// Loading content by defaut action
Gepard.ProcessAction = function (Action,Params){

this.Caller_ID=Action;
this.Caller_Params=Params;
if(Params=="&")params="";
var link=this.API+"?ProcessAction="+Action+Params;
parent.location.hash=Action.replace("&","#a-");

var GetContentError=false;
var query = $.ajax({
url: link,
error: function(request,text){ GetContentError=true; alert("error in ajax query: '"+link+"',\n\ndescription: "+text); },
async: false
});
var data=query.responseXML;
Gepard.HandleContent(data);
}

/*
 * Gets Content by action and returns it
 */
Gepard.GetContent = function (Action,Params){

var link=this.API+"?ResponseType=html&GetAction="+Action+"&"+Params;
var GetContentError=false;
var data = $.ajax({
url: link,
error: function(request,text){ GetContentError=true; alert("error in ajax query: '"+link+"',\n\ndescription: "+text); },
async: false
}).responseText;
return (GetContentError?false:data);
}



/*
 *  Handling functions
 */
Gepard.handleSubData=function(data,area){
	$(data).find("item").each(function () {
		var id=$(this).attr("id");
		var text=$(this).text();
		
		var object=document.getElementById("function_"+id);
		
		if(object==null){
			var a=$("#output_"+area);
			$(a).hide();
			a.empty();
			a.append(text);
			$(a).fadeIn();
		}
		
		else {
			$("#function_"+id).empty();
			$("#function_"+id).append(text);
		}
		
	});
	
}
Gepard.HandleContent=function(data)
	{
	
	/*
	 * Checking for redirections
	 */
		 
	$(data).find("redirect").each( function() { var link=$(this).text(); Gepard.Redirect(link);   } );

	
	/*
	 * Normal Handling
	 */
	
	$(data).find("items").each( function() {
		var text=$(this).find("data").get();
		var area=$(this).find("area").text();
		
		Gepard.handleSubData(text,area);
					
	});
	
	$(data).find("ext").each(function() {
		loadextjs($(this).text());
		});
	$(data).find("extcss").each(function() {
		loadjscssfile($(this).text(),"css");
		});
		
	}


/*
 * Other functions
 */
Gepard.Redirect=function(link){
	
	document.body.innerHTML="<div id='jloading' style='position:fixed;top:0px;right:0px;z-index:2000;'><span style='color:white;background-color:#990000;padding:2px;'>Redirection...</span></div>";
	
	window.location=link;
}

//var data = Gepard.GetContent("users-enter",null);
//alert(data);